package com.dream.utils.io;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;

import com.dream.utils.system.cfg.SystemConfigUtils;

public class ObjectIOUtils {
	
	private static final String RESULT_PATH = "result";
	private static final String SALASH = "/";
	private static final String EXTENTSION = ".object";
	private static final String PATH ;
	
	static {
		PATH  = SystemConfigUtils.instance().getValue("io.object.file.path");
	}
	
	public static String newObjectFilePath(String subPath,String uuid) {
		
		StringBuilder builder = new StringBuilder();
		builder.append(PATH);
		builder.append(subPath);
		builder.append(SALASH);
		builder.append(uuid);
		builder.append(EXTENTSION);
		return builder.toString();
		
	}
	
	public static ObjectOutputStream writeResultObjectFile(String uuid) throws IOException{
		
		return writeObjectFile(RESULT_PATH,uuid);
		
	}
	
	public static ObjectInputStream readResultObjectFile(String uuid) throws IOException{
		
		return readObjectFile(RESULT_PATH,uuid);
		
	}
	
	public static ObjectOutputStream writeObjectFile(String subPath , String uuid) throws IOException{
		
		String fileName = newObjectFilePath(subPath,uuid);
		OutputStream fileOutputStream = FileIOUtils.fileOutputStream(fileName);
		ObjectOutputStream out = new ObjectOutputStream(fileOutputStream);
		
		return out;
	}
	
	public static ObjectInputStream readObjectFile(String subPath ,String uuid) throws IOException{
		
		String fileName = newObjectFilePath(subPath,uuid);
		ObjectInputStream in = new ObjectInputStream(FileIOUtils.fileInputStream(fileName));
		return in;
		
	}
	
	public static ObjectOutputStream writeObjectFile(String path) throws IOException{
		
		OutputStream fileOutputStream = FileIOUtils.fileOutputStream(path);
		ObjectOutputStream out = new ObjectOutputStream(fileOutputStream);
		
		return out;
	}
	
	public static ObjectInputStream readObjectFile(String path) throws IOException{
		
		ObjectInputStream in = new ObjectInputStream(FileIOUtils.fileInputStream(path));
		return in;
		
	}
	
}
